#!/bin/bash
#
#  COPYRIGHT 2004, EMULEX CORPORATION
#  3333 Susan Street, Costa Mesa, CA 92626
#  
#  All rights reserved.  This computer program and related documentation
#  is protected by copyright and distributed under licenses restricting
#  its use, copying, distribution and decompilation.  This computer
#  program and its documentation are CONFIDENTIAL and a TRADE SECRET
#  of EMULEX CORPORATION.  The receipt or possession of this program
#  or its documentation does not convey rights to reproduce or disclose
#  its contents, or to manufacture, use, or sell anything that it may
#  describe, in whole or in part, without the specific written consent
#  of EMULEX CORPORATION.  Any reproduction of this program without
#  the express written consent of EMULEX CORPORATION is a violation
#  of the copyright laws and may subject you to criminal prosecution.
# 

platform_os=`uname -s`
mode_of_operation=0
change_mode_of_operation="n"

install_called=$1


# first stop two daemons
echo
echo "Stopping HBAnyware daemons ..."
/usr/sbin/hbanyware/stop_hbanyware 
echo

stay_in_loop=1
while [ $stay_in_loop -eq 1 ]
do 
  	echo
    echo "Select desired mode of operation for HBAnyware"
  	echo
	echo  "   1   Local Mode  : HBA's on this Platform can be managed by"
	echo  "                     HBAnyware clients on this Platform Only."
	echo  "   2   Managed Mode: HBA's on this Platform can be managed by"
	echo  "                     local or remote HBAnyware clients."
	echo  "   3   Remote Mode : Same as '2' plus HBAnyware clients on this"
	echo  "                     Platform can manage local and remote HBA's."
    echo
  	echo -n "Enter the number '1' or '2' or '3' "

    # Obtain user selection for HBAnyware mode of operation
    read mode_of_operation
    if [ $mode_of_operation -eq 1 ];then
        echo "You selected: 'Local Only Mode' "
#        echo "(HBA's on this Platform can be managed by HBAnyware apps on this Platform Only)"

	    # Turn off discovery daemon
        if [ "$platform_os" = "Linux" ];then
	     	chkconfig --level 2345 ElxDiscSrv off
		else
            if [ "$platform_os" = "SunOS" ];then
                if [ -f "/etc/rc2.d/S99ElxDiscSrv" ]; then
	       	        mv /etc/rc2.d/S99ElxDiscSrv /etc/rc2.d/No.S99ElxDiscSrv  
			    fi
			fi
		fi
		# RMServer DAEMON ALWAYS ON 
		# Turn off rmserver daemon
        #if [ "$platform_os" = "Linux" ];then
	    #    chkconfig --level 2345 ElxRMSrv off
	    #else
        #    if [ "$platform_os" = "SunOS" ];then
        #       if [ -f "/etc/rc2.d/S99ElxRMSrv" ]; then
	    #   	       mv /etc/rc2.d/S99ElxRMSrv /etc/rc2.d/No.S99ElxRMSrv  
		#	   fi
	    #	fi
	    #fi
        stay_in_loop=0
    else
        if [ $mode_of_operation -eq 2 ];then
            echo "You selected: 'Managed-only Mode' "
#        	echo "(HBA's on this Platform can be managed by local or remote HBAnyware apps)."

	        # Turn off discovery daemon
            if [ "$platform_os" = "Linux" ];then
	         	chkconfig --level 2345 ElxDiscSrv off
	       	else
                if [ "$platform_os" = "SunOS" ];then
                    if [ -f "/etc/rc2.d/S99ElxDiscSrv" ]; then
	           	        mv /etc/rc2.d/S99ElxDiscSrv /etc/rc2.d/No.S99ElxDiscSrv  
	       		    fi
	       		fi
	       	fi
	       	# Turn on rmserver daemon
            if [ "$platform_os" = "Linux" ];then
	            chkconfig --level 2345 ElxRMSrv on
	        else
                if [ "$platform_os" = "SunOS" ];then
                    if [ -f "/etc/rc2.d/No.S99ElxRMSrv" ]; then
	       	            mv /etc/rc2.d/No.S99ElxRMSrv /etc/rc2.d/S99ElxRMSrv
	       	        fi      
				fi
	        fi
            stay_in_loop=0
		else
            if [ $mode_of_operation -eq 3 ];then
                echo "You selected: 'Remote Mode' "
#        	    echo "(HBA's on this Platform can be managed by local or remote HBAnyware apps"
#        	    echo " and HBAnyware apps on this Platform can manage local and remote HBA's)."
	 	
	           	# Turn on discovery daemon
                if [ "$platform_os" = "Linux" ];then
	              	chkconfig --level 2345 ElxDiscSrv on
	           	else
                    if [ "$platform_os" = "SunOS" ];then
                        if [ -f "/etc/rc2.d/No.S99ElxDiscSrv" ]; then
	           		        mv /etc/rc2.d/No.S99ElxDiscSrv /etc/rc2.d/S99ElxDiscSrv  
		       			fi
	           		fi
	           	fi
                
		       	# current design has discovery daemon starting by 1st running of application
                # unless AUTOSTART entry in /etc/emulexDiscConf is true
#                /usr/sbin/hbanyware/start_elxdiscovery	&

	           	# Turn on rmserver daemon
                if [ "$platform_os" = "Linux" ];then
	                chkconfig --level 2345 ElxRMSrv on
	            else
                    if [ "$platform_os" = "SunOS" ];then
                       if [ -f "/etc/rc2.d/No.S99ElxRMSrv" ]; then
	           	          mv /etc/rc2.d/No.S99ElxRMSrv /etc/rc2.d/S99ElxRMSrv 
	           	       fi    
		       		fi
	            fi
                stay_in_loop=0
	 		else
                stay_in_loop=1
                echo
                echo "Invalid Entry"
                echo "Must Enter either '1', '2', or '3'"
	 	    fi
	 	fi
	fi 
done

if [ -z $install_called ]; then
    install_called=0
fi
# if called by installation routine; ask this question
if [ $install_called -eq 45 ];then
    # obtain user selection for change mode permission
    stay_in_loop=1
    while [ $stay_in_loop -eq 1 ]
    do 
       echo
       echo "Do you want to allow user to change management mode using" 
       echo "set_operating_mode script located in /usr/sbin/hbanyware ?"
       echo
       echo
       echo -n "Enter the letter 'y' if yes, or 'n' if no "

       # Obtain user selection for HBAnyware mode of operation
       read change_mode_of_operation
       if [ "$change_mode_of_operation" = "y" ];then
           echo "You selected: Yes "
           stay_in_loop=0;
       fi
       if [ "$change_mode_of_operation" = "Y" ];then
           echo "You selected: Yes "
           stay_in_loop=0;
       fi
       if [ "$change_mode_of_operation" = "n" ];then
           echo "You selected: NO "
           stay_in_loop=0;
       fi
       if [ "$change_mode_of_operation" = "N" ];then
           change_mode_of_operation	= "n"
           echo "You selected: NO "
           stay_in_loop=0;
       fi
       if [ $stay_in_loop -eq 1 ];then
           echo "Invalid Selection "
       fi
    done
fi


# write remote operation option to server config file  
TF=/etc/emulexRMConfig.tmp
cat /etc/emulexRMConfig | sed "/RemoteOperation/d" > $TF
if [ $mode_of_operation -eq 1 ];then
   echo "ServerRemoteOperation:disabled" >> $TF
else
   echo "ServerRemoteOperation:enabled" >> $TF
fi
cp $TF /etc/emulexRMConfig
rm $TF

# write remote operation option and mode change permission option to client config file  
TF=/etc/emulexDiscConfig.tmp
TF2=/etc/emulexDiscConfig.tmp2
cat /etc/emulexDiscConfig | sed "/RemoteOperation/d" > $TF
cat $TF | sed "/OperationMode/d" > $TF2
if [ $mode_of_operation -eq 3 ];then
   echo "ClientRemoteOperation:enabled" >> $TF2
else
   echo "ClientRemoteOperation:disabled" >> $TF2
fi
if [ "$change_mode_of_operation" = "y" ];then
   echo "ClientOperationMode:enabled" >> $TF2
else
   if [ "$change_mode_of_operation" = "Y" ];then
      echo "ClientOperationMode:enabled" >> $TF2
   else
      echo "ClientOperationMode:locked" >> $TF2
   fi
fi

cp $TF2 /etc/emulexDiscConfig
rm $TF
rm $TF2

# if mode changes allowed; init related flag to same selection
if [ "$change_mode_of_operation" = "Y" ];then
   TF=/etc/emulexRMOptions.tmp
   cat /etc/emulexRMOptions | sed "/LocalHBAsOnly/d" > $TF
   if [ $mode_of_operation -eq 3 ];then
      echo "LocalHBAsOnly:false" >> $TF
   else
      echo "LocalHBAsOnly:true" >> $TF
   fi
   cp $TF /etc/emulexRMOptions
   rm $TF
fi
if [ "$change_mode_of_operation" = "y" ];then
   TF=/etc/emulexRMOptions.tmp
   cat /etc/emulexRMOptions | sed "/LocalHBAsOnly/d" > $TF
   if [ $mode_of_operation -eq 3 ];then
      echo "LocalHBAsOnly:false" >> $TF
   else
      echo "LocalHBAsOnly:true" >> $TF
   fi
   cp $TF /etc/emulexRMOptions
   rm $TF
fi


# current design is remote server daemon is ALWAYS started
/usr/sbin/hbanyware/start_elxhbamgr &

